package articles

import (
	"blog/app/model/articles"
	"blog/app/services/system"
	Core "blog/core"
	"errors"
)

func (receiver *Cate) GetLists() ([]articles.Cate, error) {
	var cate []articles.Cate
	Core.DbConnect.Model(receiver.ArticleCateModel).Find(&cate)
	return cate, nil
}

func (receiver *Cate) GetInfo(id int) (*articles.Cate, error) {
	var info articles.Cate
	if Core.DbConnect.Model(receiver.ArticleCateModel).Scopes(system.Condition.ArticleCate.IdFind(id)).Find(&info).RowsAffected == 0 {
		return nil, errors.New("记录不存在")
	}

	return &info, nil
}

func (receiver *Cate) DelInfo(id int) error {
	db := Core.DbConnect.Model(receiver.ArticleCateModel).Scopes(system.Condition.ArticleCate.IdFind(id))
	var count int64
	if db.Count(&count); count == 0 {
		return errors.New("分类不存在")
	}

	return db.Delete(&articles.Cate{}).Error
}

func (receiver *Cate) Create(param articles.Cate) error {
	return Core.DbConnect.Create(&param).Error
}

func (receiver *Cate) Edit(id int, param articles.Cate) error {
	var count int64
	db := Core.DbConnect.Model(receiver.ArticleCateModel).Scopes(system.Condition.ArticleCate.IdFind(id))
	if db.Count(&count); count == 0 {
		return errors.New("分类不存在")
	}
	return db.Updates(param).Error
}
